//动态级联菜单
$(function () {
	//获取选中的地区ID
    $("#InputDomain1").change(function () {
        var selected_domain = $('#InputDomain1').children('option:selected').val();
        getBridgeByID(selected_domain);
    })
	//获取选中的桥梁ID
    $("#InputBridge1").change(function () {
		var selected_domain = $('#InputBridge1').children('option:selected').val();
		getSensorByID(selected_domain);
    })
	//确定按钮点击提交
	$("#submitThreshold").click(function () {
		//添加表单地址
		$("#addThresholdForm").attr("action","/setting/addThreshold").submit();
    })
	///获取删除按钮
	$(".badge-danger").click(function () {
		//console.log($(this).attr("id"));
		var clickedThrehold = $(this).attr("id").split("e")[1];
		if(confirm("确认删除？") == true){
			//确认删除后,删除ID
			//console.log(clickedThrehold);
			deleteThresholdByID(clickedThrehold);
		}
    })
})
function deleteThresholdByID(ID) {
	//根据ID进行AJAX请求
	//console.log(ID);
    $.ajax(
        {
            url: "/setting/deleteThresholdByID",
            type: "POST",
            async:false,
            dataType: "json",
            data: {TID: ID},
            success: function (result) {

            }
        });
   window.location.reload();
}
function getSensorByID(ID) {
	//ajax获取传感器
    $.getJSON("/bridge/bridgesensor/"+ID,function (result) {
        readSensor(result);
    })
}
function readSensor(JsonResult) {
    var addBridgeHtml = "<option value='-1'>请选择传感器</option>";
    $.each(JsonResult,function (i,item) {
        addBridgeHtml += "<option value='"+ item["paraId"] +"'>"+ item["cdName"]+"</option>"
    })
    //console.log(addBridgeHtml);
    $("#InputSensor1").html(addBridgeHtml);
}
function getBridgeByID(ID) {
	//ajax获取所有当前地区的桥梁
   $.getJSON("/bridge/domainbridge/"+ID,function (result) {
	   readBridge(result);
   })
}
function readBridge(JsonResult) {
	//console.log(JsonResult);
	var addBridgeHtml = "<option value='-1'>请选择桥梁</option>";
	$.each(JsonResult,function (i,item) {
        addBridgeHtml += "<option value='"+ item[0] +"'>"+ item[4]+"</option>"
    })
	//console.log(addBridgeHtml);
	$("#InputBridge1").html(addBridgeHtml);
}
//进行搜索
$(function () {
    $("#searchThresholdButton").click(function () {
        var ThresholdName = $("#SearchContent").val();
        window.location.href = "/setting/SearchThreshold/"+ThresholdName;
    })
})